<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Setting_model
 *
 * @author admin
 */
class setting_model extends CI_Model {

    private $_table_stock = 'vpc_catalog_autopart';
    private $_table_truck = 'vpc_catalog_truck';

    public function get_list_autopart_main_type() {
        $this->db->select('id');
        $this->db->select('name');
        $main_type = $this->db->get_where($this->_table_stock, array('parent_id' => NULL));
        return $main_type->result();
    }

    public function get_list_autopart_sub_type($id) {
        $this->db->select('id');
        $this->db->select('name');
        $sub_type = $this->db->get_where($this->_table_stock, array('parent_id' => $id));
        return $sub_type->result();
    }

    public function get_list_autopart_brand($id) {
        $this->db->select('id');
        $this->db->select('name');
        $brand = $this->db->get_where($this->_table_stock, array('parent_id' => $id));
        return $brand->result();
    }

    public function get_autopart_type_by_id($id) {
        $this->db->select('a.name as name');
        $this->db->select('b.name as parent_name');
        $this->db->limit('1');
        $this->db->join($this->_table_stock . ' as b', 'b.id = a.parent_id', 'left');
        $result = $this->db->get_where($this->_table_stock . ' as a', array('a.id' => $id));
        return $result->result();
    }

    public function get_truck_type_by_id($id) {
        $this->db->select('a.name as name');
        $this->db->select('b.name as parent_name');
        $this->db->limit('1');
        $this->db->join($this->_table_truck . ' as b', 'b.id = a.parent_id', 'left');
        $result = $this->db->get_where($this->_table_truck . ' as a', array('a.id' => $id));
        return $result->result();
    }

    public function save_autopart_type($parent_id = NULL, $type_name) {
        $arr_type = array('parent_id' => $parent_id,
            'name' => $type_name,
            'created_by' => $this->session->userdata('username'),
            'created_date' => date("Y-m-d H:i:s")
        );

        $this->db->insert($this->_table_stock, $arr_type);
    }

    public function update_autopart_type($id = NULL, $type_name) {
        $arr_type = array('name' => $type_name,
            'updated_by' => $this->session->userdata('username'),
            'updated_date' => date("Y-m-d H:i:s")
        );

        $this->db->update($this->_table_stock, $arr_type, array('id' => $id));
    }

    public function get_list_truck_brand() {
        $this->db->select('id');
        $this->db->select('name');
        $main_type = $this->db->get_where($this->_table_truck, array('parent_id' => NULL));
        return $main_type->result();
    }

    public function get_list_truck_model($id) {
        $this->db->select('id');
        $this->db->select('name');
        $sub_type = $this->db->get_where($this->_table_truck, array('parent_id' => $id));
        return $sub_type->result();
    }

    public function save_truck_type($parent_id = NULL, $type_name) {
        $arr_type = array('parent_id' => $parent_id,
            'name' => $type_name,
            'created_by' => $this->session->userdata('username'),
            'created_date' => date("Y-m-d H:i:s")
        );

        $this->db->insert($this->_table_truck, $arr_type);
    }

    public function update_truck_type($id = NULL, $type_name) {
        $arr_type = array('name' => $type_name,
            'updated_by' => $this->session->userdata('username'),
            'updated_date' => date("Y-m-d H:i:s")
        );

        $this->db->update($this->_table_truck, $arr_type, array('id' => $id));
    }

    public function delete_autopart($id = NULL) {
        $this->db->delete($this->_table_stock, array('id' => $id));
    }

    public function delete_truck_type($id = NULL) {
        $this->db->delete($this->_table_truck, array('id' => $id));
    }

}
